package com.cjx.admin.controller;

import com.cjx.admin.api.AdminResourceService;
import com.cjx.admin.dto.AdminResourceDTO;
import com.cjx.admin.dto.AdminResourceNodeDTO;
import com.cjx.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
*@Description 资源管理控制层
*@Verson v1.0.0
*@Author cjunxian
*@Date
*/
@Api(tags = "资源管理控制层",value = "AdminResourceController")
@RestController
@RequestMapping("adminResource")
public class AdminResourceController {

    @Autowired
    private AdminResourceService adminResourceService;

    /**
     * 根据主键id查询
     *
     * @param id 主键id
     * @return result 结果对象
     */
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "主键id", dataType = "String")})
    @ApiOperation("根据主键查询")
    @GetMapping("/selectById")
    public CommonResult<AdminResourceDTO> selectById(@RequestParam String id) {
        return CommonResult.success(adminResourceService.selectById(id));
    }

    /**
     * 批量删除
     *
     * @param ids 主键id，多个都好分隔
     * @return result 结果对象
     */
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "ids", value = "主键id，多个逗号分隔", dataType = "String")})
    @ApiOperation("批量删除")
    @PostMapping(value = "/deleteBatchIds")
    public CommonResult<Integer> deleteBatchIds(@RequestParam List<String> ids) {
        int i = adminResourceService.deleteByIds(ids);
        return CommonResult.success(i);
    }

    /**
     * 新增或修改资源
     *
     * @param adminResourceDTO 实体传输类
     * @return result 结果对象
     */
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "adminResourceDTO", value = "实体传输类", dataType = "AdminResourceDTO")})
    @ApiOperation("新增或修改资源")
    @PostMapping(value ="saveOrUpdate")
    public CommonResult saveOrUpdate(@RequestBody @Validated AdminResourceDTO adminResourceDTO) {
        boolean result;
        if(StringUtils.isEmpty(adminResourceDTO.getId())){
            result = adminResourceService.insert(adminResourceDTO);
        }else{
            result = adminResourceService.update(adminResourceDTO);
        }
        return CommonResult.success(result);
    }

    /**
     * 返回资源树
     */
    @ApiOperation("获取资源树")
    @GetMapping(value = "getResourceTree")
    public CommonResult<List<AdminResourceNodeDTO>> getResourceTree(){
        return CommonResult.success(adminResourceService.getResourceTree());
    }


}
